#!/usr/bin/env python
# -*- coding: utf-8 -*-
# --------------------------------------------------------
# Descripttion:
# version: 0.0.1
# Author: Wenwei Fu (2516800961@qq.com)
# FilePath: /fastapi_byd/app/api/routes/expert.py
# Create: 2021-01-04 16:37:40
# LastAuthor: Wenwei Fu
# lastTime: 2021-01-10 15:01:42
# --------------------------------------------------------

import uuid
import datetime
from typing import Optional
from pydantic import BaseModel
from fastapi import APIRouter, Request, Body, Depends, HTTPException
from app.db.schemas import Traffic_Cameras # 摄像头
from app.db.database import get_db

router = APIRouter()

@router.get("/list", name="查询摄像头列表", dependencies=[Depends(get_db)])
async def cameras_list(
    pageNum: Optional[int] = 1,
    pageSize: Optional[int] = 10,
    request: Request=None
):
    query = Traffic_Cameras.select().order_by(Traffic_Cameras.created_at).paginate(pageNum, pageSize).dicts()
    results = [item for item in query]
    count = Traffic_Cameras.select().count()
    return {
        "code": 200,
        "message": "查询成功",
        "data": {
            "pageNum": pageNum,
            "pageSize": pageSize,
            "total": count,
            "records": results
        }
    }

@router.get("/selectlist", name="查询摄像头列表", dependencies=[Depends(get_db)])
async def cameras_selectlist(
    road_id: str,
    request: Request=None
):
    query = Traffic_Cameras.select().where(Traffic_Cameras.camera_road_id == road_id).dicts()
    results = [item for item in query]
    return {
        "code": 200,
        "message": "查询成功",
        "data": results
    }

@router.get("/oneinfo", name="查询摄像头单个信息", dependencies=[Depends(get_db)])
async def cameras_oneinfo(
    camera_id: str,
    request: Request=None
):
    results = Traffic_Cameras.get(Traffic_Cameras.camera_id == camera_id)
    # results = [item for item in query]
    print('结果', results.__data__)
    return {
        "code": 200,
        "message": "查询成功",
        "data": results.__data__
    }

@router.get("/dicts", name="查询摄像头 字典", dependencies=[Depends(get_db)])
async def cameras_dicts(
    request: Request=None
):
    query = Traffic_Cameras.select().order_by(-Traffic_Cameras.created_at).dicts()
    results = [item for item in query]
    return {
        "code": 200,
        "message": "查询成功",
        "data": results
    }

class Create_Camera_Item(BaseModel):
    camera_name: str
    camera_type: Optional[str] = '枪球机'
    camera_lonlat: Optional[str] = ''
    camera_road_id: str
    camera_position: str
    camera_scene: Optional[str] = ''
    camera_video_url: Optional[str] = ''
    camera_original_image_url: Optional[str] = ''
    camera_image_url: Optional[str] = ''
    created_at: Optional[str] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    updated_at: Optional[str] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

@router.post("/create", name="创建摄像头", dependencies=[Depends(get_db)])
async def create_cameras(
    Create_Camera_Item: Create_Camera_Item,
    request: Request = None
):
    results = Create_Camera_Item.__dict__
    results['camera_id'] = str(uuid.uuid4())
    Traffic_Cameras.create(**results)
    print('创建摄像头', results)

    return {
        "code": 200,
        "message": "创建成功",
        "data": results
    }

class Update_Camera_Item(BaseModel):
    camera_id: str
    camera_name: str
    camera_type: str
    camera_lonlat: Optional[str] = ''
    camera_road_id: str
    camera_position: str
    camera_scene: Optional[str] = ''
    camera_video_url: Optional[str] = ''
    camera_original_image_url: Optional[str] = ''
    camera_image_url: Optional[str] = ''
    created_at: Optional[str] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    updated_at: Optional[str] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

@router.put("/update", name="更新摄像头", dependencies=[Depends(get_db)])
async def update_cameras(
    Update_Camera_Item: Update_Camera_Item,
    request: Request = None
):
    results = Update_Camera_Item.__dict__
    results['updated_at'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    updatesql = (Traffic_Cameras.update(
                    camera_name=results['camera_name'],
                    camera_type=results['camera_type'],
                    camera_lonlat=results['camera_lonlat'],
                    camera_road_id=results['camera_road_id'],
                    camera_position=results['camera_position'],
                    camera_scene=results['camera_scene'],
                    camera_video_url=results['camera_video_url'],
                    camera_original_image_url=results['camera_original_image_url'],
                    camera_image_url=results['camera_image_url'],
                    updated_at=results['updated_at']
                ).where(Traffic_Cameras.camera_id == results['camera_id']).execute())

    if updatesql == 1:
        return {
                "code": 200,
                "message": "更新成功",
                "data": {
                    "camera_id": results['camera_id'],
                    "cameras_name": results['camera_name']
                }
            }
    else:
        return {
                "code": 201,
                "message": "更新失败",
                "data": {
                    "camera_id": results['camera_id'],
                    "cameras_name": results['camera_name']
                }
            }

@router.delete("/delete", name="删除摄像头", dependencies=[Depends(get_db)])
async def delete_cameras(
    id: str,
    request: Request=None
):
    query = Traffic_Cameras.delete().where(Traffic_Cameras.id == id).execute()
    if query == 1:
        return {
            "code": 200,
            "message": "删除成功",
            "data": {
                "id": id
            }
        }
    else:
        return {
            "code": 200,
            "message": "删除失败",
            "data": {
                "id": id
            }
        }